// -------------------------------------------------------------------------
//    @FileName         :    NFCNetServerModule.h
//    @Author           :    GaoYi
//    @Date             :    2017-10-16
//    @Module           :    NFCNetServerModule
//
// -------------------------------------------------------------------------


#include "NFCNetModule.h"
#include "NFCNetServerModule.h"

#include "NFComm/NFPluginModule/NFIPlugin.h"

NFCNetServerModule::NFCNetServerModule(NFIPluginManager* p)
{
    mnBufferSize = 0;
    pPluginManager = p;
    for (int i = 0; i < NF_SERVER_TYPES::NF_ST_MAX; ++i)
    {
        NFINetModule* pNetModule = pPluginManager->CreateAloneModule<NFINetModule>();
        mxServerMap.AddElement(i, pNetModule);
    }
}

bool NFCNetServerModule::Init()
{
    return true;
}

bool NFCNetServerModule::AfterInit()
{
    return true;
}

bool NFCNetServerModule::BeforeShut()
{
    return true;
}

bool NFCNetServerModule::Shut()
{
    mxServerMap.ClearAll();
    return true;
}

bool NFCNetServerModule::Execute()
{
    return true;
}

int NFCNetServerModule::ExpandBufferSize(const unsigned int size)
{
    if (size > 0)
    {
        mnBufferSize = size;
    }
    return mnBufferSize;
}

int NFCNetServerModule::Initialization(const NF_SERVER_TYPES eType, const unsigned int nServerID, const unsigned int nMaxClient, const unsigned short nPort, const int nCpuCount)
{
    NFINetModule* pNetModule = GetServerNetInfo(eType);
    if (pNetModule == nullptr)
    {
        return -1;
    }

    return pNetModule->Initialization(nMaxClient, nPort, nCpuCount);
}

int NFCNetServerModule::AddReceiveCallBack(const NF_SERVER_TYPES eType, const int nMsgID, NET_RECEIVE_FUNCTOR_PTR functorPtr)
{
    NFINetModule* pNetModule = GetServerNetInfo(eType);
    if (pNetModule)
    {
        pNetModule->AddReceiveCallBack(nMsgID, functorPtr);
    }

    return 0;
}

int NFCNetServerModule::AddReceiveCallBack(const NF_SERVER_TYPES eType, NET_RECEIVE_FUNCTOR_PTR functorPtr)
{
    NFINetModule* pNetModule = GetServerNetInfo(eType);
    if (pNetModule)
    {
        pNetModule->AddReceiveCallBack(functorPtr);
    }

    return 0;
}


int NFCNetServerModule::AddEventCallBack(const NF_SERVER_TYPES eType, NET_EVENT_FUNCTOR_PTR functorPtr)
{
    NFINetModule* pNetModule = GetServerNetInfo(eType);
    if (pNetModule)
    {
        pNetModule->AddEventCallBack(functorPtr);
    }

    return 0;
}

NFINetModule* NFCNetServerModule::GetServerNetInfo(const NF_SERVER_TYPES eType)
{
    return mxServerMap.GetElement(eType);
}

